package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.World;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.tweens.misc.VarTween;
	import net.flashpunk.Sfx;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import flash.external.ExternalInterface;
	/**
	 * © 2012 TheJavaArcade.com
	 */	
	public class GameWorld extends World {
		//game entities
		public static var paddle:Paddle;
		public static var cursor:Cursor;
		public static var ball:Ball;
		//corners
		public static var corner_sprite:Image = new Image(Assets.CORNER_SPRITE);
		public static var corner1:Entity;
		public static var corner2:Entity;
		public static var corner3:Entity;
		public static var corner4:Entity;
		//walls
		public static var wall:Vector.<Wall> = new Vector.<Wall>;
		//game vars
		public static var ballOut:Boolean;
		public static var gameOver:Boolean;
		public static var level:int;
		public static var segments:int;
		public static var score:int;
		public static var time:int;
		private static var timerDelay:int;
		public  var bonus:int;
		public static var isBonus:Boolean;
		//top bar text
		public static var timeText:Textbox;
		public static var scoreText:Textbox;
		public static var levelText:Textbox;
		//enter name text
		public static var game_overText:Textbox;
		public static var enter_nameText:Textbox;
		public static var press_enterText:Textbox;
		//bonus message text
		public static var equationText:Textbox;
		public static var bonusText:Textbox;
		//other text
		public static var clickText:Textbox;
		//sounds		
		public static var addNote:int;
		public static var theMusic:Music;
		
		public function GameWorld() {
			add(new SoundToggle);
			GameWorld.cursor = new Cursor();
			add(cursor);
			GameWorld.paddle = new Paddle();
			add(paddle);
			GameWorld.ball = new Ball();
			add(ball);
			corner1 = new Entity(0, 20, corner_sprite);
			corner2 = new Entity(590, 20, corner_sprite);
			corner3 = new Entity(590, 470, corner_sprite);
			corner4 = new Entity(0, 470, corner_sprite);
			add(corner1); add(corner2); add(corner3); add(corner4);
			GameWorld.theMusic = new Music();
			add(theMusic);
			//configure bonus text
			bonusText = new Textbox(24, "center", false, "BONUS");
			add(bonusText);
			bonusText.x = FP.halfWidth - (bonusText.theText.textWidth/2);
			bonusText.y = 200;
			equationText = new Textbox(16, "center", false);
			add(equationText);
			equationText.y = 250
			//configure game over text
			game_overText = new Textbox(48, "left", false, "Game Over");
			game_overText.x = FP.halfWidth - (game_overText.theText.textWidth / 2);
			game_overText.y = 150;
			add(game_overText);
			enter_nameText = new Textbox(24, "left", false, "Enter your name");
			enter_nameText.x = FP.halfWidth - (enter_nameText.theText.textWidth / 2);
			enter_nameText.y = 200;
			add(enter_nameText);
			press_enterText = new Textbox(24, "left", false, "Press Enter when complete");
			press_enterText.x = FP.halfWidth - (press_enterText.theText.textWidth / 2);
			press_enterText.y = 300;
			add(press_enterText);
			//configure status bar
			timeText = new Textbox();
			add(timeText);
			timeText.x = 200;
			timeText.y = 1;
			scoreText = new Textbox();
			add(scoreText);
			scoreText.x = 380;
			scoreText.y = 1;
			levelText = new Textbox();
			add(levelText);
			levelText.x = 50;
			levelText.y = 1;
			//click to start
			GameWorld.clickText = new Textbox(16, "left", false, "click to launch");
			clickText.x = FP.halfWidth - clickText.theText.textWidth/2;
			clickText.y = FP.halfHeight;
			add(clickText);
			//initial var settings
			time = 0;
			score = 0;
			level = 1;
			bonus = 0;
			ballOut = false;
			isBonus = false;
			gameOver = false;
			//begin game
			setLevel();
		}
		public function setLevel():void {//SETLEVEL ========================================
			if (level == 1) { setWalls(4); time = 200; FP.screen.color = 0X313053; }
			if (level == 2) { setWalls(6); time = 300; FP.screen.color = 0X313053; }
			if (level == 3) { setWalls(8); time = 400; FP.screen.color = 0X313053; }
			if (level == 4) {setWalls(10); time = 500;FP.screen.color = 0X313053;}
			if (level == 5) {setWalls(12); time = 600;FP.screen.color = 0X313053;}
			if (level == 6) {setWalls(14); time = 700;FP.screen.color = 0X313053;}
			if (level == 7) {setWalls(16); time = 800;FP.screen.color = 0X533030;}
			if (level == 8) {setWalls(18); time = 900;FP.screen.color = 0X533030;}
			if (level == 9) {setWalls(20); time = 1000;FP.screen.color = 0X533030;}
			if (level == 10) { setWalls(22); time = 1100; FP.screen.color = 0X533030;}
			if (level == 11) { setWalls(24); time = 1200; FP.screen.color = 0x4a3053;}
			if (level == 12) { setWalls(26); time = 1400; FP.screen.color = 0x4a3053;}
			if (level == 13) { setWalls(28); time = 1600; FP.screen.color = 0x4a3053;}
			if (level == 14) { setWalls(30); time = 1800; FP.screen.color = 0x4a3053; }
			if (level == 15) { setWalls(32); time = 2000; FP.screen.color = 0x4a3053;}
			//turn off music
			theMusic.isPlaying = false;
			//update status bar
			timeText.theText.text = "Time " + time;
			levelText.theText.text = "Level " + level;
			scoreText.theText.text = "Score " + score;
			//remove bonus stuff
			bonus = 0;
			//set up entities for new level
			clickText.visible = true;
			paddle.x = 280;
			paddle.y = 230;
			paddle.initialPaddleMove = false;
		}
		public function setWalls(segs:int):void {//SETWALLS ========================================
			segments = segs;//total number of wall segments in the level
			var yellowCount:int = 0;//state 1
			var redCount:int = 0;//state 0
			var random:int;//hey! it's gonna be a random number!
			var wallState:int = 0;
			for (var i:int = 0; i < segs; i++) {
				random = FP.rand(segs);
				if (random < (segs / 2) - redCount + yellowCount)	wallState = 0;
				if (random >= (segs / 2) - redCount + yellowCount) wallState = 1;
				GameWorld.wall[i] = new Wall(segs, i, wallState);
				add(wall[i]);
				if (wallState == 1) yellowCount++;
				if (wallState == 0) redCount++;
			}
		}
		public function checkLevel():void {//CHECKLEVEL	========================================
			var isEqual:Boolean = true;
			for (var i:int = 0; i < segments; i++) {
				if (wall[i].state != wall[0].state) isEqual = false;
			}
			if (isEqual) {//all walls are the same color
				ballOut = false;
				isBonus = true;
			}
		}
		public function levelBonus():void {//LEVELBONUS ========================================
			//3 if statements to speed up the bonus calculation, in case there is a lot of time left on the clock
			if (time < 101 && time > 0) {
				bonus = bonus + 10;			
				time--;
			}
			if (time > 100 && time < 301) {
				bonus = bonus + 40;			
				time = time - 4;
				timerDelay = 0;
			}
			if (time > 300) {
				bonus = bonus +80;
				time = time - 8;
			}
			timeText.theText.text = "Time "+time;
			equationText.theText.text = time + " x 10 = " + bonus;
			bonusText.visible = true;
			equationText.visible = true;
			equationText.x = FP.halfWidth - (equationText.theText.width/2);
			//a little delay before the next level starts
			if (time == 0) {
				timerDelay++;
				if (timerDelay > 100) {
					score = score + bonus;
					isBonus = false;
					bonusText.visible = false;
					equationText.visible = false;
					timeText.theText.color = 0xffffff;
					level++;
					if (level == 16) {
						gameOver = true;
						add(new NameText);
						game_overText.theText.text = "Congratulations!";
						game_overText.x = FP.halfWidth - (game_overText.theText.textWidth / 2);
						game_overText.visible = true;
						enter_nameText.visible = true;
						press_enterText.visible = true;
					}
					if (level!=16){
						for (var i:int = 0; i < segments; i++) remove(wall[i]);//remove old walls
						setLevel();
					}
				}
			}
		}
		public function levelTimer():void {//LEVELTIMER ========================================
			timerDelay++;
			if (timerDelay > 9) {
				timerDelay = 0;
				time--;
				timeText.theText.text = "Time "+time;
			}
			if (time < 50) timeText.theText.color = 0Xec1010; else timeText.theText.color = 0xffffff;
			if (time == 0) {
				gameOver = true;
				add(new NameText);
				game_overText.theText.text = "Game Over";
				game_overText.visible = true;
				enter_nameText.visible = true;
				press_enterText.visible = true;
			}
		}
	//UPDATE
		override public function update():void {
			super.update();	
		//gameplay
			if (ballOut && !gameOver) {
				checkLevel();
				levelTimer();
			}
		//calculate bonus	
			if (!ballOut && isBonus) levelBonus();
		//submit name & score
			if (Input.pressed(Key.ENTER) && gameOver == true) {
				try {
					
				}catch (e:Error) { }
				
				FP.world = new TitleWorld;
			}
		}
		
	//RENDER	
		override public function render():void {
			super.render();
			//boundary of where the paddle can move
			Draw.rectPlus(40, 60, 520, 380, 0xFFFFFF, .15, false, 1);
		}
		
	}

}